package treenode;

import java.util.LinkedList;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2022/11/8
 * Time: 20:08
 */
public class Hy {
    
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        
        TreeNode(int val) {
            this.left = null;
            this.right = null;
            this.val = val;
            
        }
    }
    
    public TreeNode buildTree(TreeNode root, int[] nums, int index) {
        if (index >= nums.length) {
            return null;
        }
        root = new TreeNode(nums[index]);
        root.left = buildTree(root.left, nums, 2 * index + 1);
        root.right = buildTree(root.right, nums, 2 * index + 2);
        return root;
    }
    
    public void levelPrint(TreeNode root){
        if(root == null)
        {
            return ;
        }
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode current = null;
        queue.offer(root);//将根节点入队
        while(!queue.isEmpty())
        {
            current = queue.poll();//出队队头元素并访问
            System.out.print(current.val+" ");
            if(current.left != null)//如果当前节点的左节点不为空入队
            {
                queue.offer(current.left);
            }
            if(current.right != null)//如果当前节点的右节点不为空，把右节点入队
            {
                queue.offer(current.right);
            }
        }
        
    }
}
